/*================================================================*
        Copyright (C) 2021 All rights reserved www.hsby.link
      	�ļ����ƣ�test_address.cpp
      	�� �� �ߣ���ɭ����
      	�������ڣ�2021/12/9
 *================================================================*/

#include <arpa/inet.h>
#include <iostream>
#include <bitset>

#include "address.h"
#include "log.h"

void TestCreateMask() {
    auto mask = hsby::CreatePreMask<uint32_t>(24);
    mask = hsby::ByteSwapOnLittleEndian(mask);
    char buf[20];
    inet_ntop(AF_INET, &mask, buf, sizeof(buf));
    //HSBY_LOG_SYSTEM_DEBUG << buf;
}

void TestIpv4Address() {
    auto addr = hsby::Ipv4Address::Create("114.114.114.114",80);
    auto broacast_addr = addr->BroacastAddress(24);
    if (broacast_addr) {
        //HSBY_LOG_SYSTEM_DEBUG << broacast_addr->ToString();
    }
    auto network_addr = addr->NetworkAddress(24);
    if (network_addr) {
        //HSBY_LOG_SYSTEM_DEBUG << network_addr->ToString();
    }
    auto subnet_addr = addr->SubnetMaskAddress(24);
    if (subnet_addr) {
        //HSBY_LOG_SYSTEM_DEBUG << subnet_addr->ToString();
    }
}

void TestIpv6Address() {
    auto addr = hsby::Ipv6Address::Create("fe80::641c:f98:2c1:a428",80);
    //HSBY_LOG_SYSTEM_DEBUG << addr->ToString();
    auto broacast_addr = addr->BroacastAddress(112);
    if (broacast_addr) {
        //HSBY_LOG_SYSTEM_DEBUG << broacast_addr->ToString();
    }
    auto network_addr = addr->NetworkAddress(112);
    if (network_addr) {
        //HSBY_LOG_SYSTEM_DEBUG << network_addr->ToString();
    }
    auto subnet_addr = addr->SubnetMaskAddress(112);
    if (subnet_addr) {
        HSBY_LOG_SYSTEM_DEBUG << subnet_addr->ToString();
    }
}

void TestBaseAddress() {
//    auto addr = hsby::Ipv6Address::Create("fe80::641c:f98:2c1:a428",80);
//    HSBY_LOG_SYSTEM_DEBUG << addr->ToString();
    std::vector<hsby::BaseAddress::Ptr> addrs;
    bool ret = hsby::BaseAddress::LookUp(addrs, "hsby.link");
    if (!ret) {
        //HSBY_LOG_SYSTEM_DEBUG << "LookUp fail";
    }
    for (auto it : addrs) {
        //HSBY_LOG_SYSTEM_DEBUG << *it;
    }
    std::vector<hsby::BaseAddress::InterfaceInfoUnit> gia_res;
    if (!hsby::BaseAddress::GetInterfaceAddresses(gia_res, AF_UNSPEC)) {
        //HSBY_LOG_SYSTEM_DEBUG << "hsby::BaseAddress::GetInterfaceAddresses fail";
    }
    for (auto it : gia_res) {
        //HSBY_LOG_SYSTEM_DEBUG << HSBY_VARS_EXP(it.name, it.address, it.prefix);
    }
}

void TestIpAddress() {
    auto addr = hsby::IpAddress::Create("127.0.0.1", 80);
    HSBY_LOG_SYSTEM_DEBUG << *addr;
}

int main() {
    TestBaseAddress();
    return 0;
}